/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-8-24 上午11:06 Created by lyyang
 */
package com.jade.bss.base.blacklist.impl;

import java.util.Date;
import java.util.List;
import javax.ejb.TransactionAttribute;
import javax.inject.Inject;
import javax.inject.Named;

import com.jade.bss.base.blacklist.BlackList;
import com.jade.bss.base.blacklist.BlackListDao;
import com.jade.bss.base.blacklist.BlackListManager;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.util.PagingList;
import org.springframework.transaction.annotation.Transactional;

/**
 * 黑名单管理器
 *
 * @author <a href="mailto:lyyang@ceno.cn">lyyang</a>
 * @version 1.0 15-8-24 上午11:06
 */
@Named ("bss_blackListManager")
public class BlackListManagerImpl implements BlackListManager
{
     private BlackListDao blackListDao;


    @Inject
    public void setBlackListDao(@Named ("bss_blackListDao")BlackListDao blackListDao)
    {
        this.blackListDao = blackListDao;
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public long add(BlackList blackList){
        blackList.setCreationTime(new Date());
        BlackList old = get(blackList.getOwnerType(),blackList.getOwnerId(),blackList.getBlockType(),blackList.getBlockId());
        if (old==null){
            return  blackListDao.add(blackList);
        }
        else{
            return old.getId();
        }
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void update(BlackList blackList){
        blackListDao.update(blackList);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void remove(BlackList blackList){
        blackListDao.remove(blackList);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void remove(long... ids){
        for (long id:ids){
            BlackList blackList = get(id);
            if (blackList!=null){
                remove(blackList);
            }
        }
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void remove(String ownerType,String ownerId,String blackType,String blackId){
        blackListDao.remove(ownerType,ownerId,blackType,blackId);
    }

    public BlackList get(String ownerType,String ownerId,String blackType,String blackId){
        return blackListDao.get(ownerType,ownerId,blackType,blackId);
    }

    public boolean inBlackList(String ownerType,String ownerId,String blackType,String blackId){
        return blackListDao.get(ownerType,ownerId,blackType,blackId)!=null;
    }
    public BlackList get(long id){
        return blackListDao.get(id);
    }

    public PagingList<BlackList> query(Criteria criteria){
        return blackListDao.query(criteria);
    }

    public List<BlackList> list(Criteria criteria){
        return blackListDao.list(criteria);
    }
}
